package com.wlw.admin.openeye.ui.activity

import android.support.v7.widget.LinearLayoutManager
import com.hazz.kotlinmvp.utils.WatchHistoryUtils
import com.wlw.admin.openeye.Constants
import com.wlw.admin.openeye.R
import com.wlw.admin.openeye.base.BaseActivity
import com.wlw.admin.openeye.mvp.model.bean.HomeBean
import com.wlw.admin.openeye.ui.adapter.WatchHistoryAdapter
import com.wlw.admin.openeye.utils.StatusBarUtil
import kotlinx.android.synthetic.main.activity_watch_history.*
import kotlinx.android.synthetic.main.layout_recyclerview.*
import java.util.*

class WatchHistoryActivity : BaseActivity() {
    private var itemListData = ArrayList<HomeBean.Issue.Item>()

    companion object {
        private val HISTORY_MAX = 20
    }

    override fun layoutId(): Int = R.layout.activity_watch_history

    override fun initData() {
        multipleStatusView.showLoading()
        itemListData = queryWatchHistory()
    }

    override fun initView() {
        toolbar.setNavigationOnClickListener {
            finish()
        }
        val mAdapter = WatchHistoryAdapter(this, itemListData)
        mRecyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerView.adapter = mAdapter
        multipleStatusView.apply {
            if (itemListData.size > 1)
                showContent()
            else
                showEmpty()
        }
        StatusBarUtil.apply {
            darkMode(this@WatchHistoryActivity)
            setPaddingSmart(this@WatchHistoryActivity, toolbar)
//            setPaddingSmart(this@WatchHistoryActivity, mRecyclerView)
        }
    }

    override fun start() {
    }

    private fun queryWatchHistory(): ArrayList<HomeBean.Issue.Item> {
        val watchList = ArrayList<HomeBean.Issue.Item>()
        val hisAll = WatchHistoryUtils.getAll(Constants.FILE_WATCH_HISTORY_NAME, this)
        val keys = hisAll.keys.toTypedArray()
        Arrays.sort(keys)
        val keyLength = keys.size
        val hisLength = if (keyLength > HISTORY_MAX) HISTORY_MAX else keyLength
        (1..hisLength).mapTo(watchList) {
            WatchHistoryUtils.getObject(Constants.FILE_WATCH_HISTORY_NAME, this, keys[keyLength - it]) as HomeBean.Issue.Item
        }
        return watchList
    }
}
